package io.github.dunwu.spring.data.tx.propagation;

import io.github.dunwu.spring.data.tx.UserEntity;
import io.github.dunwu.spring.data.tx.UserRepository;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

@Service
@Slf4j
public class SubUserService {

	@Autowired
	private UserRepository userRepository;

	@Transactional
	public void createSubUserWithExceptionWrong(UserEntity entity) {
		log.info("createSubUserWithExceptionWrong start");
		userRepository.save(entity);
		throw new RuntimeException("invalid status");
	}

	/**
	 * {@link Propagation#REQUIRES_NEW} 表示执行到这个方法时需要开启新的事务，并挂起当前事务
	 */
	@Transactional(propagation = Propagation.REQUIRES_NEW)
	public void createSubUserWithExceptionRight(UserEntity entity) {
		log.info("createSubUserWithExceptionRight start");
		userRepository.save(entity);
		throw new RuntimeException("invalid status");
	}

}
